<template>
  <div class="home">
    <div class="home-banner">
      <div class="main">
        <h5>欢迎来到新疆工程建设云</h5>
        <InputSearch
            class="home-banner__search"
            v-model:value="search"
            size="large"
            enter-button="搜 索"
            @search="handlePressEnter"
            placeholder="请输入您要查询的关键词，例如ca、聘用等"
        >
          <template #prefix>
            <SearchOutlined :style="{ fontSize: '20px' , color : '#fff'}"/>
          </template>
        </InputSearch>
      </div>


    </div>
  </div>

  <div class="al-main-tab">
    <div class="main">
      <span
        v-for="(item, index) in titles"
        :key="index"
        :class="`${itemType == item.key ? 'active' : ''}`"
        @click="spanTabHandler(item.key)"
        >{{ item.label }}</span
      >
    </div>
  </div>

  <div class="pb100 main">
    <div class="al-main-box">
      <p class="al-total">为您搜索到相关结果<span class="blue">{{page.total}}</span>条</p>
      <List
        item-layout="horizontal"
        :data-source="list"
        :pagination="page"
        :loading="loading"
      >
        <template #renderItem="{ item }">
          <ListItem>
            <a target="_blank" :href="`../articleDetail/${item.article_id}`" class="al-list-item">
              <h4>{{ item.article_title }}</h4>
              <p class="nowrap">
                <span class="mr20">{{ parseTime(item.publish_time)  }}</span>
                <span>{{ item.publish_unit  }}</span>
              </p>
            </a>
          </ListItem>
        </template>
      </List>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, reactive, toRefs, unref } from "vue";
import { Input, Row, Col, List } from "ant-design-vue";
import { useRoute } from "vue-router";
import { SearchOutlined, DoubleRightOutlined } from "@ant-design/icons-vue";
import { getArticleListApi  } from '/@/api/article';
import type { configItem } from '/@/api/common';
import { parseTime } from '/@/utils';

export default defineComponent({
  components: {
    Input, InputSearch: Input.Search,
    Row, Col, List,
    ListItem: List.Item,
    SearchOutlined,
    DoubleRightOutlined,
  },
  setup() {
    const titles = ref<configItem[]>([
      {
        key: '',
        label: '全局查询'
      },
      {
        key: 'cms_index_last',
        label: '政策解读'
      },
      {
        key: '0hsr1-k62mf0-00u31',
        label: '通知'
      },
      {
        key: '0hsr1-n8sgf0-00emb',
        label: '公告'
      },
      {
        key: 'cms_index_notice',
        label: '系统动态'
      }
    ]);
    const itemType = ref<string>("");
    const search = ref<string>('')
    const { query } = toRefs(useRoute());
    search.value = unref(query).search as string || '';
    itemType.value = unref(query).itemType as string || '';
    const config = reactive({
      list: [],
      loading: false,
      page: {
        total: 0,
        current: 1,
        hideOnSinglePage: true,
        onChange:function (page:number){
          changeHandler({ current: page });
        }
      }
    })

    const spanTabHandler = (key) => {
      itemType.value = key;
      search.value = '';
      changeHandler({ current: 1 });
    }

    const handlePressEnter = () => {
      changeHandler({ current: 1 });
    }

    const changeHandler = (pagination) => {
      const { current } = pagination;
      let { loading, list } = toRefs(config);
      loading.value = true;
      const Param = {
        current: current,
        size: 10,
        itemId: itemType.value,
        searchKey:search.value
      }
      getArticleListApi(Param).then( data => {
        let { records, current, total } = data;
        list.value = records;
        loading.value = false;
        let hideOnSinglePage = true;
        config.page.current = current;
        config.page.total = total;
        config.page.hideOnSinglePage = hideOnSinglePage;
      })
    }



    const init = () => {
      changeHandler(config.page)
    };

    onMounted(init);

    return {
      titles,
      search,
      itemType,
      spanTabHandler,
      handlePressEnter,
      ...toRefs(config),
      changeHandler,
      parseTime
    };
  },
});
</script>

<style lang="less">
.al-main-box {
  .ant-list-split {
    .ant-list-item {
      padding: 0;
      border-bottom: 1px dashed #eaeaea !important;
    }
  }
}
</style>

<style lang="less" scoped>
.home-banner {
  position: relative;
  width: 100%;
  height: 480px;
  padding-top: 160px;
  background: url('/src/assets/img/home-banner.png') no-repeat center;
  background-size: cover;

  .main{

    >h5 {
      margin-bottom: 40px;
      font-size: 36px;
      line-height: 56px;
      color: @white;
    }

    >.home-banner__search {
      width: 700px;
      height: 64px;
    }
  }

}

.al-main-tab {
  height: 64px;
  border-bottom: 1px solid #eaeaea;

  span {
    position: relative;
    display: inline-block;
    padding: 0 30px;
    font-size: 15px;
    line-height: 62px;
    cursor: pointer;

    &::before {
      position: absolute;
      top: 47px;
      left: 50%;
      display: inline-block;
      width: 12px;
      height: 3px;
      margin-left: -6px;
      background: transparent;
      content: "";
      user-select: none;
    }

    &::after {
      position: absolute;
      top: 27px;
      right: 0;
      display: inline-block;
      width: 1px;
      height: 12px;
      background: #eaeaea;
      content: "";
      user-select: none;
    }

    &.active {
      color: #0078ff;

      &::before{
        background: #0078ff;
      }
    }

    &:first-child {
      padding-left: 0;

      &::before{
        margin-left: -22px;
      }
    }

    &:last-child {
      &::after {
        display: none;
      }
    }
  }
}

.al-total {
  padding-top: 24px;
  margin-bottom: 0;
  font-size: 16px;
  font-weight: 600;
}

.al-list-item {
  flex: 1;
  position: relative;
  width: 100%;
  padding: 20px 0 20px 18px;

  h4 {
    margin-bottom: 2px;
    font-size: 16px;
    font-weight: 400;
    color: inherit;
  }

  p {
    margin-bottom: 0;

    span{
      color: #999;
    }

  }

  &::after {
    position: absolute;
    top: 30px;
    left: 0;
    display: inline-block;
    width: 6px;
    height: 6px;
    background: #d6d6d6;
    border-radius: 50%;
    content: "";
  }
}

::v-deep(.ant-input-group) {
  .ant-input-search {
    background-color: rgba(255, 255, 255, 0.3);
    border: 1px solid fade(@white, 50%);

    input::placeholder {
      color: #fff;
    }
  }

  .ant-input-prefix {
    margin-right: 10px;
    margin-left: 4px;
  }

  .ant-input{
    height: 39px;
    padding-bottom: 3px;
    color: #fff;
    background: transparent;
  }

  .ant-btn-primary{
    height: 54px;
    padding: 0 35px 3px;
    background-color: #CD1922;
    border-color: #CD1922;
    border-radius: 0;
  }

  .ant-input-group-addon{
    background: transparent;
  }
}
</style>

